package com.action.mvc.auth.context;

import com.action.mvc.auth.dto.Accordion;
import com.action.mvc.auth.entity.User;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.Objects;

/**
 * Created by Wuxiang on 2017/4/24.
 */
public class UserContextFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        ResponseContext.setCurrent(response);

        if(request.getRequestURI().contains("login")){
            //删除cookie
            Cookie cookie = new Cookie("auth",null);
            cookie.setMaxAge(0);
            response.addCookie(cookie);
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        if(request.getRequestURI().contains("index.jsp")){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        if(request.getRequestURI().endsWith(".css")||request.getRequestURI().endsWith(".js")
                ||request.getRequestURI().endsWith(".jpg")||request.getRequestURI().endsWith(".gif"))
        {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        String cookieValue = "";
        if(null != request.getCookies()){
            for(Cookie cookie : request.getCookies()){
                if(Objects.equals(cookie.getName(), "auth")){
                    cookieValue = cookie.getValue();
                    break;
                }
            }

        }

        byte[] bytes = Base64.getDecoder().decode(cookieValue);
        String auth = new String(bytes);
        String[]array = auth.split("\\$");
        if (2 == array.length) {
            User user = LoginUserCache.get(array[0]);
            if (null == user) {
                LoginUserHelper helper = WebApplicationContext.getBean(LoginUserHelper.class);
                helper.executeLogin(array[0], array[1]);
                user = LoginUserCache.get(array[0]);
            }
            if (null != user && Objects.equals(user.getPwd(), array[1])) {
                List<Accordion> accordions = LoginUserCache.getAccorions(user.getName());
                request.setAttribute("accordions", accordions);
                UserContext.setCurrent(user);
                LoginUserCache.setCookie(user);
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            } else {
                Cookie cookie = new Cookie("auth", null);
                cookie.setMaxAge(0);
                response.addCookie(cookie);
                response.sendRedirect("/index.jsp");
            }
        } else {
            Cookie cookie = new Cookie("auth", null);
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
